<?php
Yii::import('application.components.CCacheAuctionDataProvider');
class AuctionPoolProvider extends AuctionPool{
    
    protected $_skipAuctionIds = array();
    public $limit = 5;
    public $search_end_time;
    public $search_category_id='live';
    public $category_id_arr;
    public $search_status = 1;
    public $offset;
    
    public $title;
    public $images;
    public $orderBy='t.end_time ASC';    
    
    public $winner_name;
    public $winner_avatar;
    public $winner_price;
    public $value_price;
    public function getUrl(){
        return Yii::app()->baseUrl.'/auction-'.$this->urlencode($this->title).'-'.$this->auction_id;
    }

    protected function urlencode($url){
        $url = str_replace(array('(',')','.','$','-',':','/','//','\\'),null,$url);
        $url=str_replace(array(' ','&','+','?','#','%','='), "_", $url);
        if(strlen($url) > 15){
            $url = substr($url,0,15);
        }
        return urlencode($url);
    }

    public function rules(){
        return array_merge(parent::rules(),array(
            array('category_id_arr,value_price,title,images,search_category_id,winner_name,winner_avatar,winner_price','safe')
        ));
    }
    
    public function getAttributes($names = null){
        $attributes = parent::getAttributes($names);
        $attributes['title'] = $this->title;
        $attributes['images'] = $this->images;
        $attributes['url'] = $this->url;
        $attributes['winner_name'] = $this->winner_name;
        $attributes['winner_avatar'] = $this->winner_avatar;
        $attributes['search_category_id'] = $this->search_category_id;
        $attributes['value_price'] = $this->value_price;
        $attributes['category_id_arr']=$this->category_id_arr;
        return $attributes;
    }
    
    public function relations(){
        return array(
            //'category'      => array(self::MANY_MANY,'Category','auction_category_relations(auction_id,category_id)','on' => 'category_category.is_delete = 0 '),
        	'category'=>array(self::HAS_MANY,'AuctionCategoryRelations','','on'=>'t.auction_id=category.auction_id')
        );
    }
    public function setNot_ids($ids){
        if (!is_array($ids)){
            $ids = explode('|', $ids);
        }
        return $this->_skipAuctionIds = $ids;
    }
    
    public function getNot_ids(){
        return $this->_skipAuctionIds;
    }
    
    public function search(){
        $criteria = new CDbCriteria;
        $criteria->order=$this->orderBy;
        $criteria->select = 'auction_id';
        
        if (!empty($this->search_category_id)){
            $criteria->with = 'category';
            $criteria->together = true;
            
        	$criteria->join=' LEFT JOIN auction_parame AS t2 on t.auction_id=t2.auction_id LEFT JOIN auction_parame AS t3 ON t3.auction_id=t.auction_id';
        	
            switch($this->search_category_id){
                    case 'live':
                        $criteria->addCondition("(t2.parame_name='show' AND t2.parame_value=1) AND (t3.parame_name='show' AND t3.parame_value=1)");
                break;
                    case 'feature':
                        $criteria->addCondition("(t2.parame_name='show' AND t2.parame_value=1) AND (t3.parame_name='feature' AND t3.parame_value=1)");
                break;	
                    case 'closed':
                        $criteria->addCondition("(t2.parame_name='show' AND t2.parame_value=1) AND (t3.parame_name='show' AND t3.parame_value=1)");
                break;
                default:
                    $criteria->addCondition("(t2.parame_name='show' AND t2.parame_value=1 ) AND (t3.parame_name='show' AND t3.parame_value=1)");
                    $criteria->addInCondition('category.category_id',$this->category_id_arr);
                break;
            }

        }
        if (!empty($this->search_end_time)){
            //$criteria->addCondition('t.end_time >= '.$this->search_end_time);
        }
        
        if (!empty($this->_skipAuctionIds)){
            $criteria->addNotInCondition('t.auction_id',$this->_skipAuctionIds);
        }
        
        if (!empty($this->offset)) {
        	$criteria->offset=$this->offset;
        }
        
        $criteria->compare('t.status',$this->search_status);
                
        return new CCacheAuctionDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>$this->limit,
            ),
        ));
    }
    
    /**
     *  获取feature  auction 
     */
    public function searchForFeature(){
        $criteria = new CDbCriteria;
        $criteria->order=$this->orderBy;
        $criteria->select = 'auction_id';
    
        if (!empty($this->search_category_id)){
            $criteria->with = 'category';
            $criteria->together = true;
            
            $criteria->join=' LEFT JOIN auction_parame AS t2 on t.auction_id=t2.auction_id';
            $criteria->compare('t2.parame_name', 'feature');
            $criteria->compare('t2.parame_value',1);
        }
        if (!empty($this->search_end_time)){
            //$criteria->addCondition('t.end_time >= '.$this->search_end_time);
        }
    
        if (!empty($this->_skipAuctionIds)){
            $criteria->addNotInCondition('t.auction_id',$this->_skipAuctionIds);
        }
    
        if (!empty($this->offset)) {
            $criteria->offset=$this->offset;
        }
        Yii::app()->db->createCommand($criteria)->text;
        $criteria->compare('t.status',$this->search_status);
        return new CCacheAuctionDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>$this->limit,
            ),
        ));
    }
}